#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include <unordered_map>
#include <unistd.h>
#include "Log.hpp"

const static std::string sep = "   ";

class Dict
{
private:
    void LoadDict()
    {
        std::ifstream in(_dictPath);
        if (!in.is_open())
        {
            LOG(FATAL, "open %s failed\n", _dictPath);
            exit(1);
        }

        std::string line;
        while (std::getline(in, line))
        {
            if (line.empty())
                continue;
            auto pos = line.find(sep);
            if (pos == std::string::npos)
                continue;
            std::string key = line.substr(0, pos);
            if (!key.size())
                continue;
            std::string value = line.substr(pos + sep.size());
            if (!value.size())
                continue;
            _dict.insert(std::make_pair(key, value));
        }
        in.close();
    }

public:
    Dict(const std::string &dictPath) : _dictPath(dictPath)
    {
        LoadDict();
    }

    std::string translate(std::string&& word)
    {
        if (word.empty())
            return "None";
        auto it = _dict.find(word);
        if (it == _dict.end())
            return "None";
        else
            return it->second;
    }

    ~Dict()
    {
    }

private:
    std::unordered_map<std::string, std::string> _dict;
    std::string _dictPath;
};

